package com.zhou.bjgl.bjgl.business_web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.bjgl.bjgl.business_web.entity.BMenu;
import com.zhou.bjgl.bjgl.business_web.mapper.BMenuMapper;
import com.zhou.bjgl.bjgl.business_web.mapper.BStudentMapper;
import com.zhou.bjgl.bjgl.business_web.mapper.BTeacherMapper;
import com.zhou.bjgl.bjgl.business_web.mapper.BUserRoleMapper;
import com.zhou.bjgl.bjgl.resp.router.BMetaResp;
import com.zhou.bjgl.bjgl.resp.router.BRouterResp;
import com.zhou.bjgl.bjgl.business_web.service.BMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author jz
 * @since 2022-3-11
 */
@Service
public class BMenuServiceImpl extends ServiceImpl<BMenuMapper, BMenu> implements BMenuService {
    @Resource
    BMenuMapper menuMapper;

    @Resource
    BUserRoleMapper userRoleMapper;

    @Resource
    BTeacherMapper bTeacherMapper;

    @Resource
    BStudentMapper bStudentMapper;

    /**
     * 根据用户权限获取路由表
     * @param roleId
     * @return
     */
    @Override
    public List<BRouterResp> getRouterByUser(Integer roleId) {
        //测试使用id为1的用户
        /*//查询该用户对应的角色
        List<BUserRole> roles = userRoleMapper.selectList(new QueryWrapper<BUserRole>().eq("user_id", id).select("role_id"));*/
        //查询角色对应的所有路由
        HashSet<BMenu> menuSet=new HashSet<>();
//        for(BUserRole userRole: roles){
//            //根据角色获取菜单
//            List<BMenu> menus=menuMapper.selectMenuByRoleId(userRole.getRoleId());
//            menuSet.addAll(menus);
//        }
        List<BMenu> bMenus = menuMapper.selectMenuByRoleId(roleId);
        menuSet.addAll(bMenus);
        //构建路由
        List<BRouterResp> routers = bulidRouters(menuSet);
        return routers;
    }



    /**
     * 构造路由表
     * @param menuSet
     * @return
     */
    private List<BRouterResp> bulidRouters(HashSet<BMenu> menuSet) {
        List<BRouterResp> routers=new ArrayList<>();
        for (BMenu menu: menuSet){
            if (menu.getPid()==1){
                //转换成路由
                BRouterResp router = createRouter(menu);
                router.setLevel(1);
                routers.add(findRouterChildren(router,menuSet));
            }
        }
        Collections.sort(routers, (o1,o2)-> o1.getId()-o2.getId());
        return routers;
    }

    /**
     * DFS 添加子路由
     * @param router 父路由
     * @param menuSet 所有路由
     * @return
     */
    private BRouterResp findRouterChildren(BRouterResp router, HashSet<BMenu> menuSet) {
        router.setChildren(new ArrayList<BRouterResp>());
        //递归添加子路由
        for(BMenu menu:menuSet){
            if (router.getId()==menu.getPid()){
                //若发现子路由
                BRouterResp routerSon = createRouter(menu);
                //把等级加1
                int level=router.getLevel()+1;
                routerSon.setLevel(level);
                if (router.getChildren()==null){
                    router.setChildren(new ArrayList<BRouterResp>());
                }
                router.getChildren().add(findRouterChildren(routerSon,menuSet));
                Collections.sort(router.getChildren(), (o1,o2)-> o1.getId()-o2.getId());
            }
        }
        return router;
    }

    /**
     * 构建路由
     * @param menu
     * @return
     */
    private BRouterResp createRouter(BMenu menu) {
        BRouterResp router=new BRouterResp();
        router.setId(menu.getId());
        router.setName(menu.getLabel());
        router.setPath(menu.getUrl());
        router.setComponent(menu.getComponent());
        if (menu.getRedirect() != null) {
            router.setRedirect(menu.getRedirect());
        }
        router.setMeta(new BMetaResp().setIcon(menu.getIcon()).setTitle(menu.getPermissionValue()));
        return router;
    }
}
